#pragma once

#include <vector>
#include <string>
#include <list>
#include <mutex>
#include "BondInfo.h"
#include "qbprotocol/include/SSQBModel.h"
#include "qbprotocol/include/SSQBQuote.h"
#include "StringPtrMap.h"

class CAdditionBondInfo;
class CABSBondInfo;
class CBondCompanyRep;
class CBondBankRep;
class CCFETSQuoteRep;
class CXCQuoteRep;
class CCFETSDealQuoteRep;
struct CFETSQUOTE;
typedef std::string CSPString;

class S_BONDLIB_EXPORT SSIntVector
{
public:
	std::vector<int> m_vct;
};

class S_BONDLIB_EXPORT BrokerKey
{
	std::string 	m_brokid;//经纪商ID
public:
	int m_nIndex;//全局容器中的索引
public:
	BrokerKey();
	BrokerKey(const std::string &szid, int nIndex);
	BrokerKey(const char* szid, int nIndex);

	BrokerKey(BrokerKey&& broker);
	BrokerKey(const BrokerKey&);
	BrokerKey& operator=(BrokerKey&& broker);
	BrokerKey& operator=(const BrokerKey& broker);

	unsigned int GetHash()const;
	bool operator==(const BrokerKey& key)const;
	const bool operator < (const BrokerKey& obj) const;
	void Clear();
	void SetBrokerId(const std::string& brokerIdStr);
	void SetBrokerId(const char* brokerIdStr);
	const std::string& GetBrokerId()const;
};

typedef std::vector<BrokerKey> CBrokerKeyVector;
class S_BONDLIB_EXPORT SSBrokerKeyVector
{
public:
	CBrokerKeyVector m_vct;
	void Insert(BrokerKey& vct){
		m_vct.push_back(vct);
	}
};

class CBondOptionInfo;
class CBondAuctionInfo;
class IntIntVec{
	// 取代std::map<int,int>
	// 理由: 1.总量内存消耗少,2.内存碎片少,3.缓存友好搜索更快
	// std::map<int,int>::_Node,包含3个指针和2个颜色char,以及2个gap,2个int,共32个字节
	// 
	struct Entry{
		int		first;
		int		second;
		Entry() :first(0), second(0){}
		Entry(int k, int v) :first(k), second(v){}
		bool operator<(const Entry& et)const{ return first < et.first; }
	};
	std::vector<Entry>	m_items;	// 按照key递增
public:
	typedef std::vector<Entry>::iterator iterator;
	typedef std::vector<Entry>::const_iterator const_iterator;
	iterator end(){ return m_items.end(); }
	const_iterator end()const{ return m_items.end(); }
	iterator begin(){ return m_items.begin(); }
	void reserve(int cap){ m_items.reserve(cap); }
	void insert(int key, int value)
	{
		insert(key)->second = value;
	}
	iterator insert(int key)
	{
		// 二分查找
		iterator it = std::lower_bound(m_items.begin(), m_items.end(), Entry(key, 0));
		if (it == m_items.end())
		{
			m_items.push_back(Entry(key, 0));
			return m_items.begin() + m_items.size() - 1;
		}
		else{
			if (it->first == key)
				return it;
			else
				return m_items.insert(it, Entry(key, 0));
		}
	}
	int& operator[](int key){
		return insert(key)->second;
	}
	const_iterator find(int key)const{
		auto it = std::lower_bound(m_items.cbegin(), m_items.cend(), Entry(key, 0));
		if (it != m_items.cend() && it->first == key)
			return it;
		return m_items.cend();
	}
};

//债券管理容器，负责管理债券
class S_BONDLIB_EXPORT CBondContainer
{
public:
	class ScopedLock{
		CBondContainer&		m_container;
	public:
		ScopedLock(CBondContainer& bc) :m_container(bc){ m_container.Lock(); }
		~ScopedLock(){ m_container.UnLock(); }
	};
	CBondContainer(const CBondContainer&) = delete;
	CBondContainer& operator=(const CBondContainer&) = delete;

private:
	CBondContainer();
public:
	static CBondContainer& instance();
	~CBondContainer();
	void RebuildMap();
	void ReserveSize(int maxBondListSize, int maxAddSize);
public://嵌套枚举类型
	// 条件类型
	enum FilterTypes {
		typeNone		=	0x00,
		typeMin			=	0x01,
		typeAll			=	0x01,
		typeBondType	=	0x02,
		typeExternal	=	0x03,
		typeAttribute	=	0x04,
		typeCoupon		=	0x05,
		typeOption		=	0x06,
		typeArgument	=	0x07,
		typeDate		=	0x08,
		typeRating		=	0x09,
		typeBondID		=	0x0A,
		typeBondKey		=	0x0B,
		typeBondExpiry	=	0x0C,
		typeMarketType	=	0x0D,
		typeBondSubType =	0x0E,
		typeBondIDInSSE =	0x0F,
		typeBondKeyInSSE=	0x10,
		typeMax			=	0x10
	};

	//产品类型
	enum ProdTypes {
		ProdNone		=	0x00,
		ProdMin			=	0x01,
		ProdAll			=	0x01,
		ProdLocal		=	0x02,
		ProdCountry		=	0x03,
		ProdCenter		=	0x04,
		ProdFinance		=	0x05,
		ProdShort		=	0x06,
		ProdChina		=	0x07,
		ProdEnterpr		=	0x08,

		ProdNCD			=	0x09,
		ProdPPN			=	0x0A,
		ProdMax			=	0x0A
	};

	//剩余期限
	enum DateRanges {
		RangeNone		=	0x00,
		RangeMin		=	0x01,
		RangeAll		=	0x01,
		Range1			=	0x02, // <= 3M
		Range2			=	0x03, // 3-6M
		Range3			=	0x04, // 6-9M
		Range4			=	0x05, // 9-12M
		Range5			=	0x06, // 1-3Y
		Range6			=	0x07, // 3-5Y
		Range7			=	0x08, // 5-7Y
		Range8			=	0x09, // 7>10Y
		Range9			=	0x0A, // >= 10Y
		RangeOther		=	0x0B,
		RangeDate		=	0x0C,
		RangeRealDate   =   0x0D,//剩余期限加号前的时间区间
		RangeMulDate	=	0x0E,//剩余期限加号前的时间区间,无剩余期限，比较期限
		RangeMax		=	0x0F,
	};

	//期限
	enum DateLineRanges {
		DL_RangeNone		=	0x00,
		DL_RangeMin			=	0x01,
		DL_RangeAll			=	0x01,
		DL_Range1			=	0x02, // 1M
		DL_Range2			=	0x03, // 3M
		DL_Range3			=	0x04, // 6M
		DL_Range4			=	0x05, // 9M
		DL_Range5			=	0x06, // 1Y
		DL_Range6			=	0x07, // 3Y
		DL_Range7			=	0x08, // 5Y
		DL_Range8			=	0x09, // 7Y
		DL_Range9			=	0x0A, // 10Y
		DL_Range10			=	0x0B, // >10Y
		DL_RangeDate		=	0x0C, //期限区间过滤
		DL_Range21			=	0x0D, // >1Y
		DL_RangeMax			=	0x0E,
	};

	//主体评级
	enum IssuerRating {
		RatingNone		=	0x00,
		RatingMin		=	0x01,
		RatingAll		=	0x01,
		RatingAAAP		=	0x02,
		RatingAAA		=	0x03,
		RatingAAP		=	0x04,
		RatingAA		=	0x05,
		RatingAAN		=	0x06,
		RatingAP		=	0x07,
		RatingOther		=	0x08,
		RatingMax		=	0x08
	};

	//中债隐含评级
	enum CDCRating
	{
		CDCRatingNone	=	0x00,
		CDCRatingMin	=	0x01,
		CDCRatingAll	=	0x01,
		CDCRatingAAAP	=	0x02,
		CDCRatingAAA	=	0x03,
		CDCRatingAAAM	=	0x04,
		CDCRatingAAP	=	0x05,
		CDCRatingAA		=	0x06,
		CDCRatingAA2	=	0x07,
		CDCRatingAAM	=	0x08,
		CDCRatingOther	=	0x09,
		CDCRatingMax	=	0x09
	};

	//中证隐含评级
	enum CSIRating
	{
		CSIRatingNone	= 0x00,
		CSIRatingMin	= 0x01,
		CSIRatingAll	= 0x01,
		CSIRatingAAAP	= 0x02,
		CSIRatingAAA	= 0x03,
		CSIRatingAAP	= 0x04,
		CSIRatingAA		= 0x05,
		CSIRatingAA2	= 0x06,
		CSIRatingAAM	= 0x07,
		CSIRatingOther	= 0x08,
		CSIRatingMax	= 0x08
	};

	//中债咨信评级
	enum CBRRating
	{
		CBRRatingNone	= 0x00,
		CBRRatingMin	= 0x01,
		CBRRatingAll	= 0x01,
		CBRRatingAAAP	= 0x02,
		CBRRatingAAA	= 0x03,
		CBRRatingAAAM	= 0x04,
		CBRRatingAAP	= 0x05,
		CBRRatingAA		= 0x06,
		CBRRatingAAM	= 0x07,
		CBRRatingAP		= 0x08,
		CBRRatingOther	= 0x09,
		CBRRatingMax	= 0x09
	};


	//展望评级
	//STB-稳定;NEG-负页;RWT-列入观察名单;POS-正面;NON-无
	enum OutlookRating
	{
		OutlookRatingNone	= 0x00,
		OutlookRatingMin	= 0x01,
		OutlookRatingAll	= 0x01,
		OutlookRatingPOS	= 0x02,//正面
		OutlookRatingSTB	= 0x03,//稳定
		OutlookRatingRWT	= 0x04,//列入观察名单
		OutlookRatingNEG	= 0x05,//负页		
		OutlookRatingMax	= 0x05,
	};
	
	//是否担保
	enum WarrantTypes {
		WarrNone		=	0x00,
		WarrMin			=	0x01,
		WarrAll			=	0x01,
		WarrPeople		=	0x02,	//担保人
		WarrObject		=	0x03,	//担保物
		WarrCRM			=	0x04,	//有缓释凭证
		WarrCLN			=	0x05,	//有联结票据
		WarrOther		=	0x06,	//其它
		WarrMax			=	0x06
	};

	//永续债
	enum ETSBondTypes
	{
		ETSBondNone = 0x00,
		ETSBondMin = 0x01,
		ETSBondAll = 0x01,
		ETSBondYes = 0x02,	//永续债
		ETSBondNo = 0x03,	//非永续债
		ETSBondMax = 0x03
	};


	//公司债
	enum CorpBondTypes
	{
		CorpBondNone = 0x00,
		CorpBondMin = 0x01,
		CorpBondAll = 0x01,
		CorpBond_Public = 0x02,	//公募
		CorpBond_Private = 0x03,//私募
		CorpBondMax = 0x03
	};

	//平台类型
	enum PlatforamTypes
	{
		PlatforamNone = 0x00,
		PlatforamMin = 0x01,
		PlatforamAll = 0x01,
		Platforam_Province = 0x02,//省
		Platforam_City = 0x03,//市
		Platforam_Area = 0x04,//区县
		Platforam_Other = 0x05,//其他
		Platforam_Max = 0x05
	};


	//地方债
	enum LocalBondTypes
	{
		LocalBondNone = 0x00,
		LocalBondMin = 0x01,
		LocalBondAll = 0x01,
		LocalBond_Normal = 0x02,	//一般债
		LocalBond_Special = 0x03,//专项债
		LocalBondMax = 0x03
	};
	//  add  by  lbl 
	//NCD   TODO 修改NCD 
	enum NCDRating
	{
		NCDRatingNone = 0x00,
		NCDRatingMin = 0x01,
		NCDRatingAll = 0x01,	//  全部
		NCDRatingSHDMCD,		//  国有 / 股份制 
		NCDRatingAAA,			//  AAA	
		NCDRatingAAP,			//  AA+
		NCDRatingAA,			//  AA
		NCDRatingAA2,			//  AA- 及以下		
		NCDRatingMax
	};




	//发行
	enum AssetStatuses {
		AssetNone = 0,
		AssetMin = 1,
		AssetAll = 1,	//全部
		AssetNull = 2,	//公募
		AssetPub = 3,	//小公募
		AssetPri = 4,	//私募
		AssetNonPri = 5,//非私募，用于上证固收平台
		AssetMax = 5
	};

	//票面
	enum CouponTypes{
		CouponNon		= 0x00,
		CouponMin		= 0x01,
		CouponAll		= 0x01,
		CouponSHIBOR	= 0x02,
		CouponDEPO		= 0x03,
		CouponLPR		= 0x04,
		CouponFIX		= 0x05,
		CouponMax		= 0x05
	};

	enum RightTypes {
		RightNone		=	0x00,
		RightMin		=	0x01,
		RightAll		=	0x01,
		RightYes		=	0x02,
		RightNo			=	0x03,
		RightMax		=	0x03
	};

	enum RightTypeEx {
		RightExNone = 0x00,
		RightExMin = 0x01,
		RightExAll = 0x01,
		RightExNo,
		RightExCal,
		RightExPut,
		RightExOther,
		RightExMax = RightExOther
	};

	//是否跨市场
	enum CrosTypes {
		CrosNone		=	0x00,
		CrosMin			=	0x01,
		CrosAll			=	0x01,
		CrosYes			=	0x02,
		CrosNo			=	0x03,
		CrosMax			=	0x03
	};

	//是否到期
	enum MaturTypes {
		MaturNone		=	0x00,
		MaturMin		=	0x01,
		MaturAll		=	0x01,
		MaturSpec		=	0x02,
		MaturPre		=	0x03,
		MaturAft		=	0x04,
		MaturMax		=	0x04
	};

	//城投
	enum MunicpTypes {
		MunicNone		=	0x00,
		MunicMin		=	0x01,
		MunicAll		=	0x01,
		MunicYes		=	0x02,	//城投	
		MunicNo			=	0x03,	//非城投
		MnuicPlatform	=   0x04,	//平台债
		MunicMax		=	0x04
	};

	//债券类型
	enum BondsType_V191 
	{
		BondNone_V191		=	0x00,
		BondMin_V191		=	0x01,
		BondAll_V191		=	0x01,
		BondCountry_V191	=	0x02,
		BondCenter_V191		=	0x03,
		BondFinance_V191	=	0x04,
		BondLocal_V191		=	0x05,
		BondShort_V191		=	0x06,
		BondChina_V191		=	0x07,
		BondEnterpr_V191	=	0x08,
		BondNCD_V191		=	0x09,
		BondOther_V191		=	0x0A,
		BondCShort_V191		=	0x0B,
		BondMax_V191		=	0x0B
	};

	//债券类型
	enum BondsType {
		BondNone =	0x00,
		BondMin =	0x01,
		BondAll =	0x01,
		BondCountry,   //国债
		BondCenter,		//央票		
		BondFinance,	//金融债
		BondLocal,		//地方债
		BondShort,		//短融
		BondChina,		//中票
		BondEnterpr,	//企业债
		BondCOP,		//公司债
		BondPPN,		//PPN    Add PPN 20160406
		BondNCD,		//NCD
		BondABS,		//ABS
		BondCRM,		//CRM
		BondConvertible,//可转债
		BondSubordinate,//次级债
		BondOther,		//其它
		BondMax = BondOther
	};

	//宁波信用债产品
	enum NbcbBondsType {
		NbcbBondNone		=	0x00,
		NbcbBondMin			=	0x01,
		NbcbBondAll			=	0x01,
		NbcbBondShort		=	0x02,	//短融
		NbcbBondSuperShort	=	0x03,	//超短融
		NbcbBondChina		=	0x04,	//中票
		NbcbBondPPN,				
		NbcbBondABS,			
		NbcbBondEnterpr,				//企业债
		NbcbBondCOP,					//公司债
		NbcbBondSecond,					//二级资本债
		NbcbBondPDB,					//熊猫债
		NbcbBondNCD,				
		NbcbBondOther,				
		NbcbBondMax				
	};

	//宁波利率债产品
	enum NbcbIRDBondsType {
		NbcbIRDBondNone		=	0x00,
		NbcbIRDBondMin		=	0x01,
		NbcbIRDBondAll		=	0x01,
		NbcbIRDBondCountry,				//国债
		NbcbIRDBondCDB,					//国开
		NbcbIRDBondLocal,				//地方债
		NbcbIRDBondOther,							
	};

	
	//民生银行债券类型 add by ke.li
	enum CmbcBondsType {
		CmbcBondNone			=	0x00,
		CmbcBondMin				=	0x01,
		CmbcBondAll				=	0x01,
		CmbcBondFinance			=	0x02,	//金融
		CmbcBondShort			=	0x03,	//短融
		CmbcBondChina			=	0x04,	//中票
		CmbcBondPrivateDebet	=	0x05,	//私募
		CmbcBondOther			=	0x06,
		CmbcBondMax				=	0x07
	};

	//超级新债通里面债券类型
	enum BondShortClassfication
	{
		BondSC_None			=	0x00,
		BondSC_Min			=	0x01,
		BondSC_All			=	0x01,
		BondSC_Country		=	0x02,
		BondSC_Center		=	0x03,
		BondSC_Finance		=	0x04,
		BondSC_Local		=	0x05,
		BondSC_Short		=	0x06,
		BondSC_CShort		=   0x07,
		BondSC_China		=	0x08,
		BondSC_Enterpr		=	0x09,
		BondSC_COP			=	0x0A,
		BondSC_NCD			=	0x0B,
		BondSC_PPN			=	0x0C,
		BondSC_ABS			=   0x0D,
		BondSC_Other		=	0x0E,
		BondSC_Max			=	0x0E
	};

	//债券类型分类
	enum ComplexBondsType
	{
		BondC_None			=	0x00,
		BondC_Min			=	0x01,
		BondC_All			=	0x01,
		BondC_Country,				//国债
		BondC_Center,				//央票
		BondC_Finance,				//金融债
		BondC_Local,				//地方债
		BondC_Short,				//短融
		BondC_CShort,				//超短融
		BondC_China,				//中票
		BondC_Enterpr,				//企业债
		BondC_COP,					//公司债
		BondC_NonPublicCor,			//非公开公司债
		BondC_NCD,					//NCD
		BondC_PPN,					//PPN				
		BondC_ABS,					//ABS
		BondC_Second,				//二级资本债
		BondC_PDB,					//熊猫债
		BondC_Other,				//其它

		//Bond 其它划分的债券类型，或者以上某几个的并集
		BondC_Prv_PPN_ABS,

		BondC_Max
	};

	//债券大分类
	enum BondLargeClassfication
	{
		BondLC_None			=	0x00,
		BondLC_InterestRate =   0x01,//利率债
		BondLC_Credit		=   0x02,//信用债
		BondLC_NCD			=   0x03,//NCD
		BondLC_PrivateDebet =	0x04,//PPN,ABS->私募债
		BondLC_PPN			=	0x05,//PPN
	};

	//发行状态分类
	enum BondIssueState
	{
		BondIssueState_None		= 0x00,//不限
		BondIssueState_Issuing	= 0x01,//发行中
		BondIssueState_Issued	= 0x02,//已发行
		BondIssueState_PreIssue	= 0x03,//预发行
	};

	//机构类型
	enum InstionTypes {
		InstNone		=	0x00,
		InstMin			=	0x01,
		InstAll			=	0x01,
		InstCentr		=	0x02,
		InstConty		=	0x03,
		InstPrvat		=	0x04,
		InstOther		=	0x05,
		InstMax			=	0x05
	};

	//常用类型
	enum CommonUseTypes {
		CommNone		=	0x00,
		CommMin			=	0x01,
		CommAll			=	0x01,				//全部
		CommElect		=	0x02,				//电力
		CommSteel		=	0x03,				//钢铁
		CommCoal		=	0x04,				//煤炭
		CommCement		=	0x05,			//水泥
		CommRealty		=	0x06,			//房地产
		CommTraffic		=	0x07,			//交运
		CommMunicipal	=	0x08,			//城投
		CommRailWay		=	0x09,			//铁道
		CommListedCompany=	0x0A,	//上市
		CommGreenBond = 0x0B,			//绿色债
		CommCarbonNeutrality = 0x0C, //碳中和
		CommMax			=	0x0C
	};

	//常用类型
	enum SettlementType {
		StmNone = 0x00,
		StmMin = 0x01,
		StmAll = 0x01,	//不限
		StmT0 = 0x02,	//T+0
		StmT1 = 0x03,	//T+1
		StmFwd = 0x04,	//远期
		StmSpot = 0x05,	//非远期
		StmMax = 0x04
	};

	//偿还方式
	enum PrincipalPayType {
		PPTNone = 0x00,
		PPTAll = 0x01,						//全部
		PPTAmortizing = 0x02,		//提前还本
		PPTNonAmortizing=0x03	//到期一次还本
	};

	//高收益债
	enum HighYieldBond {
		HYBNone = 0x00,
		HYBAll = 0x01,						 //全部
		HYBHighYield = 0x02,			 //高收益
		HYBInvestmentGrade=0x03 //非高收益
	};

	enum SortKey{
		BOND_NULL=-1,
		BOND_CODE,
		SHORT_NAME,
		EXPIRY_DATE,
		BUYVOL,
		BUYPRICE,
		SELLVOL,
		SELLPRICE,
		MODIFY_TIME,
		RATING,
		ESTPRICE,
		ESTCLEPRICE,
		BOND_TYPE,
		BUYCLEPRICE,
		SELLCLEPRICE,
		MATURITY_DATE,
		CROSSMARKET,
		BROKERKEY,
		RATING_BOND,// 债项评级
		INDICATIVE_PRICE,//参考价
		TKNPRICE,//成交价
		BSTKNPRICE,//最优报价成交价
// 		LIQUIDITY_SCORE,
// 		LIQUIDITY_RATE,
	};

	//债项评级
	enum DebtRating {
		DRatingNone		=	0x00,
		DRatingMin		=	0x01,
		DRatingAll		=	0x01,
		DRatingAAA		=	0x02,
		DRatingA_1		=	0x03,
		DRatingAAP		=	0x04,
		DRatingAA		=	0x05,
		DRatingAAN		=	0x06,
		DRatingAP		=	0x07,
		DRatingOther	=	0x08,
		DRatingMax		=	0x08
	};
	//金融债
	enum FinDebt {
		FinDebtNone		=	0x00,
		FinDebtMin		=	0x01,
		FinDebtAll		=	0x01,
		FinDebtCDB		=	0x02,//国开
		FinDebtInAndEx	=	0x03,//口行
		FinDebtAgDp		=	0x04,//农发
		FinDebtMax		=	0x04
	};

	//删除最优报价标志位
	enum LiquidationClearBestPrc
	{
		LIQ_CLR_BESTPRICE_BROKE		=	0,
		LIQ_CLR_BESTPRICE_CFETS		=	1,
		LIQ_CLR_BESTPRICE_EXCHANGE	=	2,
		LIQ_CLR_BESTPRICE_ALL,
	};


	enum EExchangeType
	{
		kExchAll,		//全部
		kExchSSE,		//上交所
		kExchSZE,		//深交所
	};
public:// 全局静态数据
	static const char lmCIB[];
	static const char lmSSE[];
	static const char lmSZE[];
public:// typedef 类型
	typedef void ( * FuncProgressProgram)(uint32_t dwCode, uint32_t dwProgress, void * lpszMsg, void * cookie);
//-------------------------------------------------------------------------
// Summary:
// 		@add by Tony 2015.2.15 新增易用性调用接口，建议优先使用新接口，为后续替换底层做准备
//-------------------------------------------------------------------------
public:
	CBondInfo*GetBond(const std::string &sBondKey, const std::string &sMarket);
	CBondInfo*GetBond(const std::string &sCombBondKey);
	CBondInfo*GetBond(int nBondIndex) ;	//考虑效率因素，不对nBondIndex会进行有效性判断,移除GetBondEx,重复多余且不用

	// 此接口被废弃掉,使用GetBondIndex和ElementAtR/ElementAtW
	//bool GetBondInfoByKeyEx(const std::string &sBondKey, const std::string &sMarket,CQBBond *&pBond,int &nBondIndex);
	//bool GetBondInfoByKeyEx(const std::string &sCombBondKey,CQBBond *&pBond,int &nBondIndex);

	const CAdditionBondInfo* GetAdditionBondInfo(int nIndex);	//获取增续发券信息
	const CABSBondInfo* GetABSBondInfo(int nBondIndex);			//获取ABS债券基础信息
	//-------------------------------------------------------------------------
	// Summary:
	// 		根据债券的key得到相应下标
	// Parameters:
	// 		sCombBondKey - 债券key
	// Returns:
	// 		下标
	//-------------------------------------------------------------------------
	int GetBondIndex(const std::string &sBondKey, const std::string &sMarket);
	int GetBondIndex(const char* sBondKey, const char* sMarket);
	int GetBondIndex(const std::string &sCombBondKey);
	bool IsValidIndex(int index);
protected:
	int GetBondIndexImpl(const char* sCombBondKey, int len = -1);
	int GetBondIndexByCodeImpl(const char* sCombBondKey, int len = -1);


////////////////////////////////////////////////////////////////////////////////////////////////////////
//-----------------------------------------------------------------------------------------------------
// 以下是旧接口

//-------------------------------------------------------------------------
// Summary:
// 		债券管理，查询等接口
//-------------------------------------------------------------------------
public:
	//-------------------------------------------------------------------------
	// Summary:
	// 		添加债券
	// Parameters:
	// 		newElement - 债券
	// Returns:
	// 		债券最后一位的下标
	//-------------------------------------------------------------------------
	int		Add( CBondInfo &newElement, CAdditionBondInfo* pAddi/*=NULL*/, CBondCompanyRep* pCompanyRep=NULL,CBondBankRep* pBankRep=NULL, CXCQuoteRep *pXCQuote = NULL, CCFETSQuoteRep* pCFETSQuote = NULL);
	int		Add(CBondInfo* pNewElement, CAdditionBondInfo* pAddi/*=NULL*/, CBondCompanyRep* pCompanyRep = NULL, CBondBankRep* pBankRep = NULL, CXCQuoteRep *pXCQuote = NULL, CCFETSQuoteRep* pCFETSQuote = NULL);
	int		AddAdditionInfo( int nBondIndex, CAdditionBondInfo* pInfo);

	int		AddBondOptionInfo(CBondOptionInfo* pOpi);
	void	RemoveBondOptionInfo(CBondOptionInfo* pOpi);
	const CBondOptionInfo* GetBondOptionInfo(int index) const;

	int		AddBondAuctionInfo(int nBondIndex, CBondAuctionInfo* pAuc);
	void	RemoveBondAuctionInfo(int nBondIndex);
	const CBondAuctionInfo* GetBondAuctionInfo(int nBondIndex);
	
	int		AddABSBondInfo(int nBondIndex, CABSBondInfo* pInfo);
	//-------------------------------------------------------------------------
	// Summary:
	// 		根据查询条件查询债券
	// Parameters:
	// 		szCode - 
	// 		szMarket - 
	// 		pInfo - 债券指针，传出参数
	// 		pid - 债券下标，传出参数
	// Returns:
	// 		若成功得到返回true,否则返回false
	//-------------------------------------------------------------------------
	bool	GetBondInfo( const char * szCode, const char* szMarket, CBondInfo * pInfo, int * pid = NULL );
	bool	GetBondInfoByKey( const char * szKey, const char* szMarket, CBondInfo * pInfo, int * pid = NULL );	//key+listedmarket查找
	bool	GetBondInfoByCombCode( const char * szCode, CBondInfo * pInfo, int * pid = NULL );		//组合后的code+listedmarket查找  szCode = ******.SH
	bool	GetBondInfoByCombKey( const char * szKey, CBondInfo * pInfo, int * pid = NULL );		//组合后的key+listedmarket查找 szKey = ********.CIB

	int		GetSize() ;											//得到债券容器大小
	int		GetSizeImpl();										//内部使用,不要在外面使用,后续会protected
	bool	Clear( );											//清除所有数据
	bool	Remove(const char * combBondKey, bool isAddition);
	bool	Remove(const char * szKey, const char* szMarket, bool isAddition);	//只对债券进行标志处理，不进行真实删除
	CBondInfo&			ElementAtW(int nIndex);					//得到债券的引用,可写版本
	const CBondInfo&	ElementAtR(int nIndex)const;			//得到债券的引用,只读版本

	//互斥信号
	bool Lock();
	bool UnLock();
	std::recursive_mutex& GetMutex(){return m_mutex;}
private:
	//-------------------------------------------------------------------------
	// Summary:
	// 		建立债券Key与债券下标的map关系
	// Parameters:
	// Returns:
	// 		
	//-------------------------------------------------------------------------
	bool	SetMap( );





/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//-----------------------------------------------------------------------------------------------------------------------
// 以下内容后期将会从BondContainer中剥离出来,与BondContainer无关的数据都将剥离出来


//-------------------------------------------------------------------------
// Summary:
// 		债券过滤函数
//-------------------------------------------------------------------------
public:

	bool	RetrieveSupperNewBonds(const BLHCONDITION & condStr,std::vector<int>& array_result);


	bool	RetrieveFromStatic( int nType, const char * szType, std::vector<int>& array_result );
	bool	RetrieveFromStatic(const BLHCONDITION & condStr, std::vector<int>& array_result, bool bSecondaryMarket = true );
	bool	RetrieveFromStaticCFETS(int nType, const char * szType, std::vector<int>& array_result);
	bool	RetrieveFromStaticSSEFI(int nType, const char * szType, std::vector<int>& array_result);

	//2016/04/27 by devin.zhang:改写的过滤函数
	bool	iRetrive(const BLHCONDITION& condStr,
					 std::vector<int>& array_result,
					 bool bSecondaryMarket = true,
					 int nCallBackType = CBondInfo::FCT_NULL,
					 _BONDFILTER BondFilter = NULL);

	static bool	GetMaturityRange(const BLHCONDITION & Cond,const CBondInfo & Info, bool bSecondaryMarket );
	static bool	GetMaturityTermRange(const BLHCONDITION & Cond,const CBondInfo & Info, bool bSecondaryMarket );//优先判断剩余期限，没时判断期限
	static bool    GetDeadLineRange(const BLHCONDITION & Cond,const CBondInfo & Info);

	static bool    GetMaturityRangeEx( const BLHCONDITION & Cond,const CBondInfo & Info); //判断剩余期限

	static bool	GetProdMultiType(const BLHCONDITION & Cond, const CBondInfo & Info);
	static bool	GetSOEenterType(const BLHCONDITION & Cond, const CBondInfo & Info);
	static bool	GetMultiLevel(const BLHCONDITION & Cond, const CBondInfo & Info);

	static bool GetCDCRatingLevel(const BLHCONDITION & Cond,const CBondInfo &Info);
	static bool GetCSIRatingLevel(const BLHCONDITION & Cond, const CBondInfo &Info);
	static bool GetCBRRatingLevel(const BLHCONDITION & Cond, const CBondInfo &Info);

	static bool GetOutlookRatingLevel(const BLHCONDITION & Cond, const CBondInfo &Info);

	static bool	GetMultiCouponType(const BLHCONDITION & Cond, const CBondInfo & Info);
	static bool	GetMultiInstion(const BLHCONDITION & Cond, const CBondInfo & Info);
	bool	GetBondClntPrice(const BLHCONDITION & Cond, int nID );
	bool	GetCompanyBidInfoById(const BLHCONDITION & Cond, int nID );//???
	bool	GetCompanyOfrInfoById(const BLHCONDITION & Cond, int nID );//???
	bool	GetBankCltBidInfoById(const BLHCONDITION & Cond, int nID );
	bool	GetBankCltOfrInfoById(const BLHCONDITION & Cond, int nID );
	bool	GetBidCndPrice(const BLHCONDITION & Cond, int nID);
	bool	GetOfrCndPrice(const BLHCONDITION & Cond, int nID);
	bool	GetBondClntdbPrice(const  BLHCONDITION & Cond, int nID);
	bool	GetDiffenceById(const BLHCONDITION & Cond, int nID);
	bool	GetBondBkPrice(const  BLHCONDITION & Cond, int nID);
	bool	GetCltOffsetEstPrc(const BLHCONDITION & Cond, int nID );
	bool	IsBilateralPrice(const  BLHCONDITION & Cond, int nID,bool bAttion = false);
	bool	IsHasPrice(const BLHCONDITION & Cond, int nID, bool bAttion = false);
	bool    IsHasPriceEx(const BLHCONDITION & Cond, int nID, bool bAttion = false);
	bool	IsCpicHasPrice(const BLHCONDITION & Cond, int nID,bool bBuyPool );
	bool    IsHyHasPrice(const BLHCONDITION & Cond, int nID );
	bool	CheckSettlement(const BLHCONDITION& Cond, int nID);
	static bool	CheckSettlement(const BLHCONDITION& Cond, const PRICE& prc);
	//  end 

	//CFETS报价筛选
	bool	IsCFETSBilateralPrice(const  BLHCONDITION & Cond, int nID);
	bool	IsCFETSHasPrice(const BLHCONDITION & Cond, int nID);
	bool	CheckCFETSSettlement(const BLHCONDITION& Cond, int nID);
	bool	GetCFETSBidCndPrice(const BLHCONDITION & Cond, int nID);
	bool	GetCFETSOfrCndPrice(const BLHCONDITION & Cond, int nID);
	bool	GetCFETSBidCdcCompare(const BLHCONDITION & Cond, int nID, bool bIsCDCAuthValid);
	bool	GetCFETSCdcOfrCompare(const BLHCONDITION & Cond, int nID, bool bIsCDCAuthValid);
	bool	GetCFETSDiffPrice(const BLHCONDITION & Cond, int nID);
	bool	GetCFETSBidVol(const BLHCONDITION& Cond, int nID);
	bool	GetCFETSOfrVol(const BLHCONDITION& Cond, int nID);

	bool	GetDiffPrice(const BLHCONDITION & Cond, int nID );
	bool	GetBondBkdbPrice(const BLHCONDITION & Cond, int nID);

	static bool GetMaturityBond(const CBondInfo & Info);
	static bool	GetRebateBond(const CBondInfo & Info);
	static long	GetDateRange(const CBondInfo & Info);

	// 新增GetModifyTimeFast接口,用于从压缩的REPORT数据中
	// 快速提取修改时间,优化排序性能,by scofined.qi
	// 见 CSSSortBondInfo::UpdateModifyTime()中使用
	static int	GetMatchType(const char* pstr);
	bool	GetCompanyInfoById(int nID);
	time_t	GetModifyTimeFast(const std::string& szKey, int nID);

	bool	GetCpicCompanyInfoById(int nID,bool bBuyPool = true);

	static bool GetMunicpType(const BLHCONDITION & Cond, const CBondInfo & Info);
	static bool	GetWarrType(const BLHCONDITION & Cond, const  CBondInfo & Info);
	static bool	GetRightType( UINT nType );

	static bool	IsETSBondType(const BLHCONDITION & Cond, const CBondInfo & Info);	 //永续债
	static bool IsCorpBondType(const BLHCONDITION & Cond, const CBondInfo & Info);//公司债

	static bool IsPlatformType(const BLHCONDITION & Cond, const CBondInfo & Info);//平台类型
	static bool IsLocalBond(const BLHCONDITION & Cond, const CBondInfo & Info);//地方债债
	
	//static bool CheckNCDRating(const BLHCONDITION& Cond, const CBondInfo& Info);//NCD   add  by  lbl

	bool	GetVolBidCompare(const BLHCONDITION & Cond, int nID);
	bool	GetVolOfrCompare(const BLHCONDITION & Cond, int nID);
	static bool    GetExchangeType(const BLHCONDITION & Cond,const CBondInfo & Info);

	bool	GetBidCdcCompare(const BLHCONDITION & Cond, int nID);
	bool	GetCdcOfrCompare(const BLHCONDITION & Cond, int nID);

	static const CSPString&	GetCouponType( UINT nType );
	static const CSPString&	GetProdType( UINT nType );
	static const CSPString&	GetRatingLevel( UINT nLevel );
	static const CSPString&	GetInstType( UINT nInst );

	bool	GetBondOffsetEstPrc(const BLHCONDITION & Cond, int nID );
	bool	IsBCQuoteValid(const BLHCONDITION & Cond, int nID );
	bool	IsQBQuoteValid(const BLHCONDITION & Cond, int nID );
	void	SetBKQuoteDiff2CDCFlag(const char* sCompanyID, int nIndex, char cBidFlag, char cOfrFlag);

	bool	GetStateType(const BLHCONDITION& Cond,const CBondInfo& Info);
	bool	GetIssueStartDate(const BLHCONDITION& Cond,const CBondInfo& Info);

	bool	GetBilateralfoById(int nID);
	bool	IsBilateralfoById(const char* sCompanyID, int nID);
	bool	GetBilateralfoByIdOverSea(int nID);

	void	ClearCDCDataAll();
	void	ClearCDCData(bool bSpecial);	//清除中债估值收益率净价久期
	void	ClearCSData();					//清除中证估值收益率净价久期


	static bool CheckPrincipalPayType(const BLHCONDITION& Cond, const CBondInfo& Info);

//-------------------------------------------------------------------------
// Summary:
// 		更新推送过来的容器数据
//-------------------------------------------------------------------------
public:
	//-------------------------------------------------------------------------
	// Summary:
	// 		更新网络消息推送数据
	// Parameters:
	// 		condStr - 过滤条件 IN 传入参数
	// 		vct_result - 债券添加数据容器 OUT 输出参数 
	// 		vct_resdel - 债券删除数据容器 OUT 输出参数
	// 		bclist - 网络报价数据，IN 传入参数
	// Returns:
	// 		若更新数据成功返回true,否则返回false
	//-------------------------------------------------------------------------
	bool	UpadateBondList(const BLHCONDITION & condStr,SSIntVector& vct_result,
			SSIntVector& vct_resdel,const std::list<xQBBondQuote_c> &bclist, bool bSecondaryMarket = true );
	bool	UpdateContainer(BLHCONDITION & condStr,SSBrokerKeyVector& vct_result,
		SSBrokerKeyVector& vct_resdel,const std::list<xQBBestPriceUnit_c> &qblist, bool bSecondaryMarket = true);
	bool	UpdateOverSeaContainer(const BLHCONDITION & condStr,SSBrokerKeyVector& vct_result,
		SSBrokerKeyVector& vct_resdel,const std::list<xQBIndicativePriceUnit_c> &qblist, bool bSecondaryMarket = true);

	//更新CFETS报价推送
	bool	UpdateContainerByCFETS(const BLHCONDITION & condStr, SSBrokerKeyVector& vct_result,
		SSBrokerKeyVector& vct_resdel, const std::list<xQBBestPriceUnit_c> &qblist, bool bSecondaryMarket = true);

	//上证固收场外市场更新报价推送
	bool	UpdateContainerBySSEFI(BLHCONDITION & condStr, SSBrokerKeyVector& vct_result,
		SSBrokerKeyVector& vct_resdel, const std::list<xQBBestPriceUnit_c> &qblist, bool bSecondaryMarket = true);

	//ABS报价推送更新
	bool	UpdateContainerByABSBond(BLHCONDITION & condStr, SSBrokerKeyVector& vct_result,
		SSBrokerKeyVector& vct_resdel, const std::list<xQBBestPriceUnit_c> &qblist, bool bSecondaryMarket = true);

//-------------------------------------------------------------------------
// Summary:
// 		最优报价版本号控制
//-------------------------------------------------------------------------
public:
	void	ClearVersion();
	void	ClearVersion(int clrFlag);
	int		GetVersionById(const std::string& szKey);
	void	UpdateVersionById(const std::string& szKey, int nVerNo, time_t nTime);

//CFETS报价版本号
	bool	CheckCFETSDayChanged();
	void	ClearCFETSDealQuoteVersion();
	int		GetCFETSDealQuoteVersion();
	bool	CheckCFETSDealDayChanged();
//-------------------------------------------------------------------------
// Summary:
// 		其它辅助函数
//-------------------------------------------------------------------------
public:
	static double CalcRemainingDay(const std::string& sPeriod, bool ytm = false);		//计算剩余时间
	static bool   CompareRemainingDay(std::string& sPeriod1,std::string& sPeriod2);

	void	CalcRemainingDate(time_t t);//更新有效债券容器
	void	CalcBondExpireInHoliday();	//判定债券到期日为节假日
	int		GetLastCalcDate();

	void	CalcIssueDateRange();		//计算最大最小发行日
	int		GetMinIssueDate();			//最小发行日
	int		GetMaxIssueDate();			//最大发行日

//-------------------------------------------------------------------------
// Summary:
// 		报价行情函数和成员函数
//-------------------------------------------------------------------------
public:
	//-------------------------------------------------------------------------
	// Summary:
	// 		根据债券BondKey和Key(公司名称)得到报价
	// Parameters:
	// 		szBondKey - 债券combBondKey
	// 		szKey -		公司名称
	// 		stRep -		报价
	// Returns:
	// 		若成功得到返回true,否则返回false
	//-------------------------------------------------------------------------
	bool	GetCompanyRepByKey(const char* szComboBondKey, REPORT& stRep);							//得到公司最优报价	
	bool	GetCompanyRepByKey(const char * szComboBondKey,const char * szBrokerId, REPORT &stRep);			//根据债券szBondKey和公司名称得到报价
	bool	GetCompanyRepByKeyAndSettlement(const char * szComboBondKey, const char * szBrokerId, BYTE btSettlement, REPORT &stRep);			//根据债券szBondKey和公司名称得到报价
	bool	GetClentQuotByKey(const char * szBondKey,const char * szKey, BKQUOTE &stQuot);			//得到Bank Client报价
	bool	GetXCQuoteByKey(const char * szBondKey, const char * szMarket, XCQUOTE &quote);			//获取交易所报价
	bool	FilterQuoteByClearSpeed(CFETSQUOTE& quote, BYTE btSettlement);
	static unsigned char GetCFETSSpeedType(const std::string& sDes);

	bool	GetCompanyRepById(int nId, REPORT& stRep);
	bool	GetCompanyRepById(int nId, const char* szBrokerId, REPORT& stRep);							//根据债券下标和公司名称得到报价
	bool	GetCompanyRepByIdAndSettlement(int nId, const char* szBrokerId, BYTE btSettlement, REPORT& stRep);			//根据债券szBondKey和公司名称得到报价

	//-------------------------------------------------------------------------
	// Summary:
	// 		根据债券下标，公司id得到报价，若公司id为空，则取所有broker报价中最新的报价
	// Parameters:
	// 		nId - 债券下标 bondIndex
	// 		szKey - 公司id
	// 		stRep - 报价
	// Returns:
	// 		若成功返回true,否则返回false
	//-------------------------------------------------------------------------
	bool	GetCompanyRepByIdEx(int nId, const char * szKey, REPORT &stRep, int nFlag); //flag = 0 取最新，1 取最新有效，2 取最新双边

	// 根据债券下标，公司id得到报价的m_company_id字段的信息
	bool    GetCompanyIdFromRep(int nId, const char * szKey, char* buf, int capcity);


	//根据时间比较 ，取时间最新的报价，太保项目      
	bool    GetLatestCompanyRepByBondIndex(int nBondIndex,REPORT &stRep,bool bBuyPool);
	//-------------------------------------------------------------------------
	// Summary:
	// 		根据Key(公司名称)得到报价
	// Parameters:
	// 		szKey - 公司Key
	// 		nID - 债券下标 (公司下标等于债券下标)
	// Returns:
	// 		报价，底层是定义了静态变量，不允许缓存，否则会出错
	//-------------------------------------------------------------------------
	bool GetCompanyInfoById(const std::string& szKey, int nID, REPORT& report);	//公司报价
	bool GetBankInfoById(const std::string &szKey, int nID, BKQUOTE& quote);		//Bank报价
	bool GetClentInfoById(const std::string &szKey, int nID, BKQUOTE& quote);	//BankClient报价

	//-------------------------------------------------------------------------
	// Summary:
	// 		根据债券下标得到报价对象指针
	// Parameters:
	// 		nIndex - 债券下标
	// Returns:
	// 		报价对象指针
	//-------------------------------------------------------------------------
	CBondCompanyRep*	GetCompanyRep(int nIndex);
	CBondBankRep*		GetBankRep(int nIndex);
	CBondBankRep*		GetClntRep(int nIndex);
	CXCQuoteRep*		GetXCQuote(int nIndex);
	CCFETSQuoteRep*		GetCFETSQuote(int nIndex);
	CCFETSDealQuoteRep* GetCFETSDealQuote(int nIndex);


	time_t	GetCompanyTimeById(const std::string& szKey, int nID); //得到公司报价更新时间

	void	ClearReport();		//清除公司，Bank和BankClient报价
	void	ClearReportLiquidation(LiquidationClearBestPrc clrFlag);//清盘时清除clrFlag对应的broker，cfets，交易所的报价
	void	ClearXCQuote();		//清除国债券期货交易所报价
	void	ClearCFETSQuote();	//清除CFETS报价
	void	ClearCFETSDealQuote();	//清除CFETS成交行情


	//-------------------------------------------------------------------------
	// Summary:
	// 		设置兴业银行冻结状态
	// Parameters:
	// 		szCompanyID - 公司名称
	// 		bFreeze - 冻结状态
	// Returns:
	// 		
	//-------------------------------------------------------------------------
	void	SetFreezeReport(const char * szCompanyID,bool bFreeze);
	bool    GetFreezeReport(const char * szCompanyID);

	//获取增续发关系表
	bool GetAdditionList(int nID, std::set<int>* &pSet);

	bool	CheckVersion();		//检查报价版本号，如果跨天，清数据，返回TRUE；否则返回FALSE

	//-------------------------------------------------------------------------
	// Summary:
	// 		查询单券是否有中债估值权限
	//-------------------------------------------------------------------------
	bool	CheckBondCdcValid(const char * szBondKey, const char * szListedMarket);
	bool	CheckBondCdcValid(const char * szCombBondKey);

public://调试用
	void DumpStatus(int mode = 0)const;

private:
	//typedef IntIntVec IntIntMap;//取代map<int,int>
	typedef StringPtrMap MapStringToPtr;
	typedef std::map<int, int> IntIntMap;
	std::vector<CBondInfo*>			m_vctbond;			//债券容器
	CBondInfo						m_bond_null;		// 永远都不能被改写
	CBondInfo						m_bond_null_write;	// 引入防止ElementAtW返回将m_bond_null改写了
	std::recursive_mutex			m_mutex;			//互斥信号
	MapStringToPtr					m_map;				// key:CBondInfo.m_sBondID 债券代码，value:m_vctbond的索引强行转化为void*
	MapStringToPtr					m_keymap;			// key:CBondInfo.m_sBondKey债券编码，value:m_vctbond的索引强行转化为void*

	std::vector<CAdditionBondInfo*> m_vctaddi;	//增续发券相关信息
	std::map<int, std::set<int>>	m_mapaddi;		//增续发券关系表

	std::vector<CBondOptionInfo*>	m_vctoptioninfo;	//含权债信息
	std::vector<CBondAuctionInfo*>	m_vctauctioninfo;	//利率债发行信息
	IntIntMap						m_mapauctioninfo;
	//time_t							m_serverTime;		//时间
	int								m_nMinIssueDate;	//最小发行日
	int								m_nMaxIssueDate;	//最大发行日
	std::map<std::string, int>		m_verarr;	// 版本号控制
	int								m_nLastReportDate;	//最新一笔报价的日期，用于判定是否跨天
	FuncProgressProgram				m_pfnProgressProgram;

	volatile int					m_nLastCalcDate; //最后一次计算剩余期限的日期

private:
	std::vector<CABSBondInfo*> m_vctabsbond;	//ABS债券数据
	IntIntMap				m_mapabsbond;	//ABS债券关系表

public:
	static void DecodeSettlement(const PRICE& price, PRICE_SETTLEMENT_LIST& pdList);
	static void DecodeSettlement(const char* interpret, PRICE_SETTLEMENT_LIST& pdList);
};
